<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="关联的学校id" prop="schoolId">
        <el-input
          v-model="queryParams.schoolId"
          placeholder="请输入关联的学校id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="通知标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入通知标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="通知发布的日期" prop="releaseDate">
        <el-date-picker clearable
          v-model="queryParams.releaseDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择通知发布的日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="通知的有效时间范围" prop="expirationDate">
        <el-date-picker clearable
          v-model="queryParams.expirationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择通知的有效时间范围">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发布通知的人" prop="issuer">
        <el-input
          v-model="queryParams.issuer"
          placeholder="请输入发布通知的人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="标记是否逻辑删除，0表示未删除，1表示已删除" prop="isDeleted">
        <el-input
          v-model="queryParams.isDeleted"
          placeholder="请输入标记是否逻辑删除，0表示未删除，1表示已删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="记录创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择记录创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="记录创建人" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入记录创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录最后修改时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择记录最后修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="记录最后修改人" prop="updatedBy">
        <el-input
          v-model="queryParams.updatedBy"
          placeholder="请输入记录最后修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:notifications:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['com/ruoyi/business:notifications:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh-right"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:notifications:remove']"
        >撤回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-view"
          size="mini"
          @click="getList(false)"
          v-hasPermi="['business:notifications:export']"
        >查看历史通知</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['com/ruoyi/business:notifications:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border stripe :header-cell-style="rowClass" v-loading="loading" :data="notificationsList" @selection-change="handleSelectionChange">
      <el-table-column :resizable="false" type="selection" width="55" align="center" />
      <el-table-column :resizable="false" label="序号" align="center" type="index" width="100" />
      <el-table-column :resizable="false" label="通知标题" align="center" prop="title" />
      <el-table-column :resizable="false" label="通知标题" align="center" prop="isClassNotification">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.isClassNotification === 0"
            type="primary"
            effect="dark"
          >班级通知</el-tag>
          <el-tag
            v-else-if="scope.row.isClassNotification === 1"
            type="primary"
            effect="dark"
          >学校通知</el-tag>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" label="通知发布的日期" align="center" prop="releaseDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" label="通知的有效时间范围" align="center" prop="expirationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" label="通知状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag effect="dark" :type="scope.row.status=='0'?'danger':'success'">
            {{ scope.row.status==0?"已过期":"已发布" }}
          </el-tag>
        </template>
      </el-table-column >
      <el-table-column :resizable="false" label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" label="通知单位" align="center" prop="departmentName" />
      <el-table-column :resizable="false" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:notifications:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh-right"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:notifications:remove']"
          >撤回</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-s-comment"
            @click="handleDelete(scope.row)"
            v-hasPermi="['com/ruoyi/business:notifications:remove']"
          >查看通知详情</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改存储通知的基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form":model="form" :rules="rules" label-width="150px">
        <!-- <el-form-item label="关联的学校id" prop="schoolId">
          <el-input v-model="form.schoolId" placeholder="请输入关联的学校id" />
        </el-form-item> -->
        <el-form-item label="通知标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入通知标题" />
        </el-form-item>
        <el-form-item label="通知类型" prop="isClassNotification">
          <el-select v-model="form.isClassNotification" placeholder="请选择通知类型">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通知发布的日期" prop="releaseDate">
          <el-date-picker
            v-model="form.releaseDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择通知发布的日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="通知有效时间范围" prop="expirationDate">
          <el-date-picker clearable
            v-model="form.expirationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择通知的有效时间范围">
          </el-date-picker>
        </el-form-item>


        <el-form-item label="通知详情" prop="contentText">
          <!-- <Editor  v-model=form.contentText></Editor> -->
          <el-input
            type="textarea"
            v-model="form.contentText"
            rows="10"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="通知单位" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入通知单位" />
        </el-form-item>
        <!-- <el-form-item label="发布通知的人" prop="issuer">
          <el-input v-model="form.issuer" placeholder="请输入发布通知的人" />
        </el-form-item>
        <el-form-item label="标记是否逻辑删除，0表示未删除，1表示已删除" prop="isDeleted">
          <el-input v-model="form.isDeleted" placeholder="请输入标记是否逻辑删除，0表示未删除，1表示已删除" />
        </el-form-item>
        <el-form-item label="记录创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择记录创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="记录创建人" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入记录创建人" />
        </el-form-item>
        <el-form-item label="记录最后修改时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择记录最后修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="记录最后修改人" prop="updatedBy">
          <el-input v-model="form.updatedBy" placeholder="请输入记录最后修改人" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 通知详情框 -->
    <el-dialog
      :title="form.title+'通知详情'"
      :visible.sync="detailsOpen"
      width="90%"
      append-to-body>
      <!-- <div>{{form.contentText}}</div> -->
      <div class="inputDeep">
        <el-input
            :readonly="true"
            type="textarea"
            autosize
            v-model="form.contentText">
          </el-input>
      </div>
      <!-- <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span> -->
    </el-dialog>
  </div>
</template>

<script>
import { listNotifications, getNotifications, delNotifications, addNotifications, updateNotifications } from "@/api/notice/notice";
export default {
  name: "Notifications",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //选中数组标题
      titles:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 存储通知的基本信息表格数据
      notificationsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示通知详情弹出层
      detailsOpen:false,
      //通知类型
      options:[{
          value: 0,
          label: '班级通知'
        },
        {
          value: 1,
          label: '学校通知'
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inform:true,
        schoolId: null,
        title: null,
        releaseDate: null,
        expirationDate: null,
        contentText:null,
        issuer: null,
        status: null,
        isDeleted: null,
        createdAt: null,
        createdBy: null,
        updatedAt: null,
        updatedBy: null,
        isClassNotification:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        departmentName:[
        { required: true, message: "通知单位不能为空", trigger: "blur" }
        ],
        isClassNotification:[
          { required: true, message: "通知类型不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "通知的主题或标题不能为空", trigger: "blur" }
        ],
        releaseDate: [
          { required: true, message: "通知发布的日期不能为空", trigger: "blur" }
        ],
        expirationDate: [
          { required: true, message: "通知的有效时间范围不能为空", trigger: "blur" }
        ],
        contentText:[
        { required: true, message: "通知的详情不能为空", trigger: "blur" }
        ],
        issuer: [
          { required: true, message: "发布通知的人不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "表示通知的当前状态，0表示未发布不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //设置table标题样式
    rowClass ({ row, rowIndex }) {
      return 'font-size: 15px; background-color: #FAFAFA;'
    },
    // 查看通知详情
    viewDetails(row){
      this.reset();
      const notificationsId = row.notificationsId || this.ids
      getNotifications(notificationsId).then(response => {
        this.form = response.data;
        this.detailsOpen=true
      });
      // console.log("=====")
      // this.detailsOpen=true
    },
    /** 查询存储通知的基本信息列表 */
    getList(inform) {
      if(!inform){
        this.queryParams.inform=inform
      }
      this.loading = true;
      listNotifications(this.queryParams).then(response => {
        this.notificationsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        notificationsId: null,
        schoolId: null,
        title: null,
        releaseDate: null,
        expirationDate: null,
        contentText:null,
        issuer: null,
        status: null,
        isDeleted: null,
        createdAt: null,
        createdBy: null,
        updatedAt: null,
        updatedBy: null,
        departmentName:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.notificationsId)
      this.titles=selection.map(item => item.title)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加存储通知的基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const notificationsId = row.notificationsId || this.ids
      getNotifications(notificationsId).then(response => {
        this.form = response.data;
        console.log("99999999=",this.form)
        this.open = true;
        this.title = "修改存储通知的基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // console.log("isClassNotification=",typeof(this.form.isClassNotification))
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log("=====",this.form)
          if (this.form.notificationsId != null) {
            updateNotifications(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotifications(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 撤回按钮操作 */
    handleDelete(row) {
      const notificationsIds = row.notificationsId || this.ids;
      const titleName = row.title || this.titles;
      // this.$modal.confirm('是否确认删除存储通知的基本信息编号为"' + notificationsIds + '"的数据项？').then(function() {
      this.$modal.confirm('是否确认撤回存储通知的基本信息标题为"' + titleName + '"的数据项？').then(function() {
        return delNotifications(notificationsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤回成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/notifications/export', {
        ...this.queryParams
      }, `notifications_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.inputDeep>>>.el-textarea__inner{
    border: 0;
    resize: none;
    padding: 0;
    font-size: 20px;
    color:#000 !important;
    cursor:default;
  }
</style>
